<?php
namespace Admin\Controller;
use Think\Controller;
use Myself\FileSystem;
use Admin\Controller\CommonController;
class AreaController extends CommonController {

	protected $statusArr=array("下架","上架");
	protected $isHome=array("未推荐","已推荐");

	
    public function index(){
		header('Content-Type:text/html;charset=utf-8');
		
		//-----------------搜索-------------------
	
		$sh_q = I('sh_q','');//关键字
		if(!empty($sh_q)){
			$shArray['sh_q'] = $sh_q;
			$map['id'] = $sh_q;
			$map['title'] = array('LIKE','%'.$sh_q.'%');
			$map['content'] = array('LIKE','%'.$sh_q.'%');
			$map['_logic'] = 'OR';
			$where['_complex'] = $map;
		}


		$from_time = I('from_time','');
		$to_time = I('to_time',''); 
		  
		if($from_time){
			$from_time_s = strtotime($from_time);
			$where['addtime']= array('EGT',$from_time_s);
			$shArray['from_time'] = $from_time;
		}
		
		if($to_time){
			$to_time_s = strtotime($to_time)+86400;
			$where['addtime']= array('ELT',$to_time_s);
			$shArray['to_time'] = $to_time;	
		}		
		
		if($from_time && $to_time){
			$where['add_time']=array(array('EGT',$from_time_s),array('ELT',$to_time_s),'and');
		}	
			  
		if(!empty($shArray)){$this->assign('shArray',$shArray);}// 搜索参数输出
		//-------------------------搜索end-----------------

		$pageno = I('p',1);
                $pageCount = 10;
		$where['level'] = 3;
		$list = M('shop_address')->where($where)->order('id desc')->page($pageno.','.$pageCount)->select();
		
		foreach( $list as $key => $item){
			$list[$key]['school'] = M('shop_address')->where('id="'.$item['id'].'"')->getField('name');
			$list[$key]['city_id'] = M('shop_address')->where('id="'.$item['pid'].'"')->getField('id');
			$list[$key]['city_pid'] = M('shop_address')->where('id="'.$list[$key]['city_id'].'"')->getField('pid');
			$list[$key]['city_name'] = M('shop_address')->where('id="'.$list[$key]['city_id'].'"')->getField('name');
			$list[$key]['pro_id'] = M('shop_address')->where('id="'.$list[$key]['city_pid'].'"')->getField('id');
			$list[$key]['pro_name'] = M('shop_address')->where('id="'.$list[$key]['pro_id'].'"')->getField('name');
		}
		
		
		$this->assign('list',$list);
		$count = M('shop_address')->where($where)->count();
		$this->assign('count',$count);	
		$Page = new \Think\Page($count,$pageCount);
		$show = $Page->showAdmin();
		$this->assign('page',$show);
                $this->assign('listAddress',$list_address);
		$this->assign('statusArr',$this->statusArr);
		$this->display('Area/index');
    }
	
	
	
	public   function select_address(){
	
	  $list_one_id = I('checkValueOne');
	  if( $list_one_id != ' '){
	  $list_two = M('shop_address')->where('pid="'.$list_one_id.'"')->select();
	  $list_str.=' <option selected="selected" value="">'.'请选择'.'</option>';
	
	 foreach($list_two as $key=>$item){
		   $list_str.=' <option  value='.$item['id'].'>'.$item['name'].'</option>';
		}
	 } else{
		 $list_str.=' <option selected="selected" value="">'.'请选择'.'</option>';
		}
		
		 $this->ajaxReturn( $list_str );
	
	}
	
	
	
	
	
	public   function select_address2(){
          $id = I('updateid');
	  $dataInfo = M('shop_address')->where('id="'.$id.'"')->find($id);
	  $dataInfo['city_id'] =  $dataInfo['pid'];
	  
	
	  $list_one_id = I('checkValueOne');
	  if( $list_one_id != ' '){
	  $list_two = M('shop_address')->where('pid="'.$list_one_id.'"')->select();
	  $list_str.=' <option selected="selected" value="">'.'请选择'.'</option>';
	
		foreach($list_two as $key=>$item){
		   $list_str.=' <option  value='.$item['id'].'>'.$item['name'].'</option>';
		}
	   } else{
		 $list_str.=' <option selected="selected" value="">'.'请选择'.'</option>';
		}
		
		 $this->ajaxReturn( $list_str );
	
	}
	
	
	
	
 public function editStatus(){
	     $cid = I('cid');
             $status = I('state');
	     $dataInfo = M('shop_news')->find($cid);
	     if( $dataInfo ){
	          if( $status == 1){
		     $data['status'] = 0;
		   }else{
		      $data['status'] = 1;
		   }
		   $result = M('shop_news')->where('id="'.$dataInfo['id'].'"')->save($data);
	     }
		 
	     if( $result ){
		 $info = 100;
		 $this->ajaxReturn($info);
		 }
	 
	 }
	 
  public  function edit(){
 
	  $id = I('id');
	  $dataInfo = M('shop_address')->where('id="'.$id.'"')->find($id);
	  $dataInfo['city_id'] =  $dataInfo['pid'];
	  $dataInfo['pro_id'] = M('shop_address')->where('id="'.$dataInfo['city_id'].'"')->getField('pid');
	  $list_address= M('shop_address')->where('level=1')->select(); 
	  $list_address_city= M('shop_address')->where('pid="'.$dataInfo['pro_id'].'"')->select(); 
	  if( $dataInfo ){
		 $this->assign('dataInfo', $dataInfo);
		 $this->assign('listAddress',$list_address);
		 $this->assign('listAddressCity',$list_address_city);
		 $this->display();
	  }

	}
	
  public  function  saveEdit(){
         $data['pid']= I('select_name6');
         $data['name'] = I('school');
	 $id = I('school_id');
         if($data){
	    $result = M('shop_address')->where('id="'.$id.'"')->save($data);
	  }
	if( $result ){
	   $this->redirect('Area/index');
	 }else{
	   $this->redirect('Area/index');
	 }
       
	    

  }	
  
  
     public function add(){
		$list_address= M('shop_address')->where('level=1')->select(); 
		$this->assign('listAddress',$list_address);
		$this->display();
    }
	
	
	public  function save(){  
	     $data['pid']= I('select_name6');
             $data['name'] = I('school');
             $data['level'] = 3;
	     if( $data ){
                $result = M('shop_address')->add($data);		 
	     }
	     if( $result ){
	        $this->redirect('Area/index');
	     }
   }
	
	 public function del(){
           $id = $_REQUEST['id'];
	    if (is_array($id)){
               $where = 'id in('.implode(',', $id).')';
            } else {
               $where = 'id='.$id;
            }
	   $list = M('shop_address')->where($where)->delete();
	   if( $list){
		 $this->redirect('Area/index');
	    }		   
      }	 
}